#include "pluginsmanagerdao.h"
#include "../ConnectionPool.h"

#include <iostream>

/* static */
QCache<QString, PluginsManager> PluginsManagerDao::pluginsManagerCache;
QCache<QString, QList<PluginsManager>> PluginsManagerDao::pluginsManagersCache;

PluginsManagerDao::PluginsManagerDao()
{

}

/* 根据id查找 */
PluginsManager *PluginsManagerDao::findOne(const quint32 &id)
{
	/* 缓存 */
	QString key = QString("findOne%1").arg(id);
	if (pluginsManagerCache.contains(key))
		return pluginsManagerCache.object(key);

	/* 组合sql语句 */
	QString content = QString("select * from `plugins_manager` where id = %1").arg(id);

	//获得查询结果
	QSqlRecord* rec = ConnectionPool::runOne(content);

	//判空
	if(rec == nullptr) return nullptr;

	//将查询的结果存入PluginsManager类的对象中
	PluginsManager* pluginsManager = new PluginsManager();
	pluginsManager->setId(rec->value("id").toUInt());

	/* 加入缓存 */
	pluginsManagerCache.insert(key, pluginsManager);

	return pluginsManager;
}

/* 根据自定义条件查找 */
PluginsManager *PluginsManagerDao::findOne(const Specification &spec)
{
	/* 缓存 */
	if (pluginsManagerCache.contains("findOne" + spec.getSqlWhere()))
		return pluginsManagerCache.object("findOne" + spec.getSqlWhere());

	//构建sql语句
	QString content = QString("select * from `plugins_manager` where %1").arg(spec.getSqlWhere());

	//获得查询结果
	QSqlRecord* rec = ConnectionPool::runOne(content);

	//判空
	if(rec == nullptr) return nullptr;

	//将查询的结果存入Plugin类的对象中
	PluginsManager* pluginsManager = new PluginsManager;
	pluginsManager->setId(rec->value("id").toUInt());

	/* 加入缓存 */
	pluginsManagerCache.insert("findOne" + spec.getSqlWhere(), pluginsManager);

	return pluginsManager;
}

QList<PluginsManager> PluginsManagerDao::findAll(const Specification &spec)
{
	if (pluginsManagersCache.contains("findAll" + spec.getSqlWhere()))
		return *pluginsManagersCache.object("findAll" + spec.getSqlWhere());

	QList<PluginsManager> res;

	//构建sql语句
	QString content = QString("select * from `plugins_manager` %1").arg(
				(spec.getSqlWhere() == "" ? "" : spec.getSqlWhere()));

	//获得查询结果
	QList<QSqlRecord> rec = ConnectionPool::runAll(content);

	//将查询的结果存入QList<PluginsManager>链表中
	for(auto it = rec.begin(); it != rec.end(); it++)
	{
		PluginsManager pluginsManager;
		pluginsManager.setId(it->value("id").toUInt());
		res.push_back(pluginsManager);
	}

	/* 加入缓存 */
	pluginsManagersCache.insert("findAll" + spec.getSqlWhere(), &res);

	return res;
}

/* 保存 */
void PluginsManagerDao::save(const PluginsManager *pluginsManager)
{
	//先判断该插件管理器数据是否存在，以主键id来判断
	QString content = QString("select * from `plugins_manager` where id = %1").arg(pluginsManager->getId());
	QSqlRecord* rec = ConnectionPool::runOne(content);

	content.clear();
	if(rec == nullptr)
	{   //如果不存在，则insert
		content = QString("insert into `plugins_manager` values(null)");
		ConnectionPool::runNo(content);
	}
	else
	{   //如果存在，则update
		content = QString("update `plugins_manager` set where id = %1").arg(pluginsManager->getId());
		ConnectionPool::runNo(content);
	}

	/* 清理缓存 */
	pluginsManagerCache.clear();
	pluginsManagersCache.clear();
}

/* 删除 */
void PluginsManagerDao::del(const quint32 &id)
{
	//先判断该插件管理器数据是否存在，以主键id来判断
	QString content = QString("select * from `plugins_manager` where id = %1").arg(id);
	QSqlRecord* rec = ConnectionPool::runOne(content);

	content.clear();
	if(rec == nullptr)
	{   //判空
		qDebug() << "无此插件管理器";
		return;
	}

	content = QString("delete from `plugins_manager` where id = %1").arg(id);
	ConnectionPool::runNo(content);

	/* 清理缓存 */
	pluginsManagerCache.clear();
	pluginsManagersCache.clear();
}
